package com.szholly.pro.watergas_server.ps.android.service;


import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 2016/12/22 <br/>
 *
 * @author Joey Huang QQ:273179370）
 */

public class QcHelper {
    //样品是否来源于采样 --实验室空白 或者 标样（质控样）不需要匹配平行样
    public static boolean isSampleFromSampling(String code) {
        return !isLabBlank(code) && !isStandard(code);
    }

    //是否为有证标样
    public static boolean isStandardTypeCertified(String count) {
        return "1".equals(count);
    }

    //实验次数 现场空白 标样（质控样）普通样 是一次
    public static int getLabCount(String code) {
        if (isLocaleBlank(code) || isStandard(code) || isNormal(code)) {
            return 1;
        } else {
            return 2;
        }
    }

    //现场平行样 采样次数为2
    public static int getSampleCount(String code) {
        if (isLocaleParallel(code)) {
            return 2;
        } else {
            return 1;
        }
    }

    //需要匹配的样品编号次数
    public static int getMatchCount(String qcType) {
        if (isLocaleBlank(qcType) || isLocaleBlank(qcType) || isNormal(qcType)) {
            return 1;
        } else if (isLabParallelPlaid(qcType) || isRecycle(qcType)) {
            return 2;
        } else if (isStandard(qcType) || isLabBlank(qcType) || isLabParallelPassword(qcType)) {
            return 0;
        } else {
            return 0;
        }
    }

    public static boolean validSampleCode(String prefix, String sampleCode) {
        Pattern pattern = Pattern.compile(prefix + "[A-Za-z0-9\\-_]{0,8}");
        Matcher match = pattern.matcher(sampleCode);
        return match.matches();
    }

    public static String getSampleCodePrefix(String sampleCode) {
        if (sampleCode == null) {
            return "";
        }
        int index = -1;
        for (int i = 0; i < sampleCode.length(); i++) {
            char c = sampleCode.charAt(i);
            if (c >= '0' && c <= '9') {
                index = i;
                break;
            }
        }
        if (index == -1) {
            return "";
        }
        return sampleCode.substring(0, index + 8);
    }

    //现场空白
    public static boolean isLocaleBlank(String qcType) {
        return "0".equals(qcType);
    }

    //现场平行样（密码）
    public static boolean isLocaleParallel(String qcType) {
        return "1".equals(qcType);
    }

    //实验室平行(明码)
    public static boolean isLabParallelPlaid(String qcType) {
        return "2".equals(qcType);
    }

    //加标回收
    public static boolean isRecycle(String qcType) {
        return "3".equals(qcType);
    }

    //标样（质控样）
    public static boolean isStandard(String qcType) {
        return "4".equals(qcType);
    }

    //实验室空白
    public static boolean isLabBlank(String qcType) {
        return "5".equals(qcType);
    }

    //普通样
    public static boolean isNormal(String qcType) {
        return "6".equals(qcType);
    }

    //实验室平行（密码）
    public static boolean isLabParallelPassword(String qcType) {
        return "7".equals(qcType);
    }
}
